Skip to content

Reduce Wasm code size with -Oz and optional LTO - #178

Merged
vishvananda merged 5 commits into
mainfrom
experiment/size-lto
Nov 18, 2026
Merged

vishvananda merged 5 commits into
mainfrom
experiment/size-lto

Conversation

@vishvananda

@vishvananda vishvananda commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Add an experimental size profile and optional link optimizations.

  • -Oz limits optimizations that grow code. It removes unused arguments, keeps more single-use expressions on the Wasm stack, and emits fewer exception handlers.
  • -flto shares identical Wasm function bodies and keeps distinct function addresses.
  • -flto=full optimizes consecutive source inputs together for Wasm and Linux x86-64. On Linux x86-64, use -flto=full -c -o combined.o to produce one object for the host linker.

The size optimizations run at -Oz. The LTO modes run only when requested and can be used at other optimization levels. The README lists these flags under Caveats.

cppgm Wasm build Raw Brotli
-O3 13.07 MiB 2.94 MiB
-Oz 8.13 MiB 2.08 MiB
-Oz -flto 8.02 MiB 2.07 MiB
-Oz -flto=full 7.32 MiB 1.89 MiB

The updated native and WASI full-LTO self-builds matched byte for byte. The Wasm experiment reproduced 244 objects and 20 linked outputs across 12 browser projects. At -O0, -O1, -O2, -O3, and -Os, 25 sampled Wasm and x86-64 outputs matched main byte for byte with LTO off.

The new Linux x86-64 tests cover cross-file inlining, weak symbols resolved by a separate object, translation-unit-local inline copies and TLS initialization, and linking a combined object with the host toolchain. The 468-source native self-build reproduced its combined object and executable byte for byte. In one single-core sample at -O3, full LTO used 489 MiB of peak memory versus 197 MiB for separate compilation and produced an 11.73 MiB executable versus 10.42 MiB. Runtime differences on eight compiler files were within timing variation.

The focused regression tests, all 11,328 strict tests, debug-info checks, the PA36 smoke suite, self-host through PA5, native inception, the harness, and the student export passed locally.

Stackify single-use integer expressions and share exception handlers across calls to the same landing pad. Keep inferred unwind effects on typed LowIR calls and enable in-place argument specialization for size builds.

Check both the exception paths and emitted Wasm structure, including the LowIR text round trip.
Lower consecutive source inputs into one LowIR program before native object generation. Also allow the ELF driver to emit one combined relocatable object for the host linker.

Reset pointer-keyed entries between translation units and keep thread-local initialization scoped to its defining unit. Validate native links, mixed object inputs, and host-linkable output, and document the experimental flags.
@vishvananda
vishvananda merged commit c49e45b into main Nov 18, 2026
49 of 54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant